using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace Loading.Models
{ 
    public class PowderRepository : IPowderRepository
    {
        LoadingContext context = new LoadingContext();

        public IQueryable<Powder> All
        {
            get { return context.Powders; }
        }

        public IQueryable<Powder> AllIncluding(params Expression<Func<Powder, object>>[] includeProperties)
        {
            IQueryable<Powder> query = context.Powders;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public Powder Find(int id)
        {
            return context.Powders.Find(id);
        }

        public void InsertOrUpdate(Powder powder)
        {
            if (powder.PowderId == default(int)) {
                // New entity
                context.Powders.Add(powder);
            } else {
                // Existing entity
                context.Entry(powder).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var powder = context.Powders.Find(id);
            context.Powders.Remove(powder);
        }

        public void Save()
        {
            context.SaveChanges();
        }
    }

    public interface IPowderRepository
    {
        IQueryable<Powder> All { get; }
        IQueryable<Powder> AllIncluding(params Expression<Func<Powder, object>>[] includeProperties);
        Powder Find(int id);
        void InsertOrUpdate(Powder powder);
        void Delete(int id);
        void Save();
    }
}